#editsAndAverages.py
#
#
#HoOC

import sys
import os
import collections
import time

def main():
	
	
	global nsList
	nsList = ["main", "talk", "user", "user talk", "project", "project talk",
		"file", "file talk", "mediawiki", "mediawiki talk", "template", "template talk", "help", "help talk",
		"category", "category talk", "portal", "portal talk", "book", "book talk", "course", "course talk",
		"institution", "institution talk"]
	path1 = sys.argv[1] #input directory to files, such as "edits/"
	path2 = sys.argv[2]
	originalfiles = os.listdir(path1)
	
	for nsfile in originalfiles:
		fileName = path1+nsfile
		percents = getPercents(fileName)
		writeToFile(percents)
		print "Finished writing: " + nsfile + " at " + str(time.localtime())

	nsfiles = os.listdir(path2)
	for nsfile in nsfiles:
		fileName = path2+nsfile
		getAverages(nsfile,path3)
		print "Finished averaging: " + nsfile + " at " + str(time.localtime())


def getPercents(fileName):
	
	f = open(fileName)
	percents = {} #namespace->percent of user u's edits (list)
	userDict = collections.defaultdict(int) #namespace-> # edits by u
	user = None 
	firstUser = True
	total = collections.defaultdict(float)	

	for line in f:
		pieces = line.split('\t')

		username = pieces[0]

		
		if username <> user and not firstUser:
			for x in range(0,int(endyear)-int(startyear)+1):				
				for ns in nsList:
					key = ns+str(x)
					if key not in userDict.keys():
						if key not in percents.keys():
							percents[key] = []
						percents[key].append(0) 
			for key in userDict.keys():
				if key not in percents:
					percents[key] = []
				offset2 = key[-1]
				percentage = float(float(userDict[key])/total[offset2])
				#appends dictionary with percents for each namespace and offset for the user				
				percents[key].append(percentage)
				
			
			userDict.clear()
			user = username
			total.clear()

		elif username <> user and firstUser:
			user = username
			
		
		namespace = pieces[2].strip()
		startyear = pieces[3].split('-')[0]
		endyear = pieces[4].split('-')[0]
		date = pieces[1].split('-')[0]
		offset = int(date)-int(startyear)
		userDict[namespace+str(offset)] += 1 #tallies number of edits for each user according to namespace and offset
		firstUser = False
		total[str(offset)] += 1 #tallies total for each offset year

	for x in range(0,int(endyear)-int(startyear)+1):				
		for ns in nsList:
			key = ns+str(x)
			if key not in userDict.keys():
				if key not in percents.keys():
					percents[key] = []
				percents[key].append(0) 	
	for key in userDict.keys():
		if key not in percents:
			percents[key] = []
		offset2 = key[-1]
		percentage = float(float(userDict[key])/total[offset2])
		percents[key].append(percentage)
	
	return percents
	
#writes the dictionary to separate files, to be average by getAverages.py
def writeToFile(percentsDict):
	for key in sorted(percentsDict.keys()):
		f = open("edits/"+key+".txt","a")
		for value in percentsDict[key]:
			f.write(str(value)+"\n")
		f.close()

def getAverages(fileName):
	total = 0
	average = 0
	num = 0
	f = open("averagesResults.txt","a")
	for value in open(fileName,"r"):
		num += float(value)
		total += 1
	average = str(num/total) #finds average of proportion 
	f.write(fileName + ": " + average)
	f.close()

main()		
